package com.indexingsystem.system.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.MenusVo;
import com.indexingsystem.boss.Vo.PavilionColumnsSortVo;
import com.indexingsystem.boss.Vo.ResCategoryOrderQueryVo;
import com.indexingsystem.boss.Vo.TreeQueryVo;
import com.indexingsystem.boss.admin.mapper.PavilionColumnsMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.MenusRelations;
import com.indexingsystem.boss.entity.PavilionColumns;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.boss.utils.StringUtil;
import com.indexingsystem.system.service.IPavilionColumnsService;

@Service
public class PavilionColumnsServiceImpl implements IPavilionColumnsService{
	
	@Resource
	private PavilionColumnsMapper pavilionColumnsMapper;
	
	@Override
	public PavilionColumns getPavilionColumnName(String name) {
		return pavilionColumnsMapper.getPavilionColumnName(name);
	}
	
	@Override
	public List<PavilionColumns> getPavilionColumns() {
		return pavilionColumnsMapper.getPavilionColumns();
	}
	
	@Override
	public Integer getPavilionColumnsList(TreeQueryVo treeQueryVo) {
		return pavilionColumnsMapper.getPavilionColumnsList(treeQueryVo);
	}

	@Override
	public PagedResult<PavilionColumns> selectPavilionColumnsList(TreeQueryVo treeQueryVo) {
		Integer pageNo = treeQueryVo.getPageNumber() == null ? 1 : treeQueryVo.getPageNumber();
		Integer pageSize = treeQueryVo.getPageSize()== null ? 10 : treeQueryVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(pavilionColumnsMapper.selectPavilionColumnsList(treeQueryVo));
	}
	
	@Override
	public MenusVo getMenusRelationsInfo(String id) {
	    MenusVo menus = null;
	    if(StringUtils.isNotBlank(id)){
	        menus = new MenusVo();
	        PavilionColumns pavilionColumns = pavilionColumnsMapper.selectPavilionColumns(id);
	        if(null != pavilionColumns){
	            menus.setPavilionColumns(pavilionColumns);
	        }
	        List<MenusRelations> menusRelations = pavilionColumnsMapper.seleteMenusRelations(id);
	        if(null != menusRelations && menusRelations.size()>0){
	            menus.setMenusRelations(menusRelations);
	        }else{
	            menusRelations = new ArrayList<MenusRelations>();
	            menus.setMenusRelations(menusRelations);
	        }
	    }
		return menus;
	}
	
	@Override
	public int updatePavilionColumns(MenusVo menusVo) throws Exception {
	    int result = 0;
	    if(null != menusVo){
	        List<MenusRelations> menusRelations = menusVo.getMenusRelations();
	        List<String> idList = null;
	        List<MenusRelations> addMenusRelationsList = null;
	        MenusRelations menusRelation = null;
	        Set<MenusRelations> menusRelationsSet = null;
	        PavilionColumns pavilionColumns = menusVo.getPavilionColumns();
	        if(null != pavilionColumns){
	        	idList = new ArrayList<String>();
	        	 if(null != menusRelations && menusRelations.size()>0){
	        	     menusRelationsSet = new HashSet<MenusRelations>();
	 	        	for (MenusRelations menus : menusRelations) {
	 		            menusRelation = new MenusRelations();           
	 		            menusRelation.setMenuId(pavilionColumns.getMenuId());
	 		            menusRelation.setResId(menus.getResId());
	 		            menusRelation.setResName(menus.getResName());
	 		            
	 		            if(!menusRelationsSet.contains(menusRelation)){
	 		               menusRelationsSet.add(menusRelation);
	 		            }
	 		        }
	 	        	if(null != menusRelationsSet && menusRelationsSet.size()>0){
	 	        	   addMenusRelationsList = new ArrayList<MenusRelations>();
	 	                for(MenusRelations menusRelaObje:menusRelationsSet){
	 	                   menusRelaObje.setMenuResId(StringUtil.createSystemDataPrimaryKey());
	 	                   addMenusRelationsList.add(menusRelaObje);
	 	                }
	 	            }
	 	        	
	 	        }
	        	 idList.add(pavilionColumns.getMenuId());
	        	 if(null != idList && idList.size()>0){
	        		 pavilionColumnsMapper.deleteMenusRelations(idList);
	        	 }
	        	 
	        	 if(null != addMenusRelationsList && addMenusRelationsList.size()>0){
	        		 pavilionColumnsMapper.insertMenusRelations(addMenusRelationsList);
	        	 }
	        	 result = pavilionColumnsMapper.updatePavilionColumns(pavilionColumns);
	        }	      
	    }
		
		return result;
	}

	@Override
	public int insertPavilionColumns(MenusVo menusVo) throws Exception {
		int result = 0;
		result = pavilionColumnsMapper.insertPavilionColumns(menusVo.getPavilionColumns());
		List<MenusRelations> menusRelationsList = menusVo.getMenusRelations();
		List<MenusRelations> addMenusRelationsList = null;
		MenusRelations menusRelation = null;
	    Set<MenusRelations> menusRelationsSet = null;
		if(null != menusRelationsList && menusRelationsList.size() > 0){
		    menusRelationsSet = new HashSet<MenusRelations>();		    
			for(MenusRelations menusRelations : menusRelationsList){
				menusRelation = new MenusRelations();
				menusRelation.setMenuId(menusVo.getPavilionColumns().getMenuId());
				menusRelation.setResId(menusRelations.getResId());
				menusRelation.setResName(menusRelations.getResName());				
			    if(!menusRelationsSet.contains(menusRelation)){
                     menusRelationsSet.add(menusRelation);
			    }				
			}			
			if(null != menusRelationsSet && menusRelationsSet.size()>0){
                addMenusRelationsList = new ArrayList<MenusRelations>();
                 for(MenusRelations menusRelaObje:menusRelationsSet){
                    menusRelaObje.setMenuResId(StringUtil.createSystemDataPrimaryKey());
                    addMenusRelationsList.add(menusRelaObje);
                 }
             }
		}
		if(null != addMenusRelationsList && addMenusRelationsList.size() > 0){
			result = pavilionColumnsMapper.insertMenusRelations(addMenusRelationsList);
		}
		return result;
	}
	
	@Override
	public int deletePavilions(List<String> list) throws Exception {
	    int result =0;
	    if(null != list && list.size()>0){
	        pavilionColumnsMapper.deleteMenusRelations(list);
	        pavilionColumnsMapper.deletePavilionMenuRelations(list);
	        result = pavilionColumnsMapper.deletePavilionColumns(list);
	    }
	    return result;
	}
	
	@Override
	public String getParentMenuSortMax(String id) {
		return pavilionColumnsMapper.getParentMenuSortMax(id);
	}

	@Override
	public PavilionColumns selectByPrimaryKey(String id) {
		return pavilionColumnsMapper.selectByPrimaryKey(id);
	}
	
    /**
     * 栏目菜单排序获取自身或目录排序栏目菜单列表.
     * 
     * @param resCategoryOrderQueryVo
     * @return
     */ 
    @Override
    public List<PavilionColumns> getPavilionColumnsByColumnOrder(ResCategoryOrderQueryVo resCategoryOrderQueryVo){
        return pavilionColumnsMapper.getPavilionColumnsByColumnOrder(resCategoryOrderQueryVo);
    }
    
    
    
    /**
     * 批量修改菜单栏目上移、下移操作.
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public int updateBatchPavilionColumns(List<PavilionColumns> list) throws Exception{
        return pavilionColumnsMapper.updateBatchPavilionColumns(list);
    }
    
    /**
     * 获取父级最小排序值.
     * @param id
     * @return
     */
    @Override
    public String getParentMenuSortMin(String id){
        return pavilionColumnsMapper.getParentMenuSortMin(id);
    }

    
    /**
     * 通过资源分类id批量删除栏目资源分类关系表.
     * @param list
     * @return
     */
    @Override
    public int deleteMenusRelationsByResCategoryIds(List<String> resCategoryIdList){
        return pavilionColumnsMapper.deleteMenusRelationsByResCategoryIds(resCategoryIdList);
    }
}